#!/usr/bin/perl
#target_mod.cgi
require './tgtman-lib.pl';

&ReadParse();

my $tname = $in{'oldfullname'};
my $mark = $in{'setaccount'};
my $user = $in{'tuser'};
my $pwd = $in{'tpwd'};
@tlist=&get_target_list();
my $found = 0;
my $tg;
foreach $t (@tlist) {
	if($t->{'fullname'} eq $tname) {
		$found = 1;
		$tg = $t;
	}
}
if(!$found){
	$err_msg = "No target selected!";
	goto ERROR_OUT;	
}
my $stat = &get_session($tg->{'fullname'});
if($stat =~ /^connected/) {
	$err_msg = "target connedted! can not modify!";
	goto ERROR_OUT;
}
if($mark eq 'y'){
        if(!&checkuser($user))
        {
                $err_msg = "Invalid username";
                goto ERROR_OUT;
        }
}
if($user){

        if(!&checkpwd($pwd))
        {
                $err_msg = "Invalid password";
                goto ERROR_OUT;
        }
}
&modify_target($tg, $in{'tuser'}, $in{'tpwd'});
$rc = `/etc/init.d/iscsi-target status`;
if($rc =~ /running/){
	`/etc/init.d/iscsi-target restart`;
}
&redirect("index.cgi");
ERROR_OUT:
&error_setup("");
&error($err_msg);
